package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.excel.model.StudentModel;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 学员列表查询（分页查询）
     *
     * @param studentQueryParam
     * @return
     */
    @Override
    public PageResult<Student> pageQuery(StudentQueryParam studentQueryParam) {
        // 设置分页参数
        PageHelper.startPage(studentQueryParam.getPage(), studentQueryParam.getPageSize());

        Page<Student> page = studentMapper.pageQuery(studentQueryParam);

        return new PageResult<>(page.getTotal(), page.getResult());
    }


    /**
     * 批量删除学员
     *
     * @param ids
     * @return
     */
    @Override
    public void delete(List<Integer> ids) {
        studentMapper.delete(ids);
    }

    /*@Override
    public void delete(Integer[] ids) {
        studentMapper.delete(ids);
    }*/


    /**
     * 添加学员
     *
     * @param student
     */
    @Override
    public void add(Student student) {
        // 补全基础属性
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(student.getCreateTime());

        studentMapper.insert(student);

    }

    /**
     * 根据学生id查询学生信息
     *
     * @param id
     * @return
     */
    @Override
    public Student getByStudentId(Integer id) {
        return studentMapper.getByStudentId(id);
    }


    /**
     * 更新学员信息（修改学员）
     *
     * @param student
     * @return
     */
    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }


    /**
     * 违纪处理
     *
     * @param id
     * @param score
     * @return
     */
    @Override
    public void violation(Integer id, Short score) {
        // 根据学生id查询学生的违纪次数和违纪分数
        Student student = studentMapper.selectCountAndScore(id);

        student.setId(id);
        student.setViolationCount((short) (student.getViolationCount() + 1));
        student.setViolationScore((short) (student.getViolationScore() + score));
        student.setUpdateTime(LocalDateTime.now());

        studentMapper.violation(student);
    }


    /**
     * 批量导入学员信息
     *
     * @param stuList
     * @param clazzId
     */
    @Override
    @Transactional
    public void importStudent(List<StudentModel> stuList, Integer clazzId) {
        /*for (StudentModel studentModel : stuList) {
            Student student = studentModel.toStudent(clazzId);
            studentMapper.insert(student);
        }*/

        //批量插入
        List<Student> studentList = stuList.stream().map(sm -> sm.toStudent(clazzId)).collect(Collectors.toList());
        studentMapper.insertBatch(studentList);
    }
}
